import {defineStore} from "pinia";
import {
    getDescriptionListApi,
    postDescriptionApi,
    putDescriptionApi
} from "@/api/modules/system/description.js";
import {ElMessage} from "element-plus";

export const useDescriptionDrawerStore = defineStore({
    id: "chestnut-descriptionDrawer",
    state: () => ({
        // 描述标题
        title: "",
        // 唯一标识
        keywords: "",
        // 显示的值
        mavonValue: "",
        // 是否展示抽屉页
        descriptionDrawer: false,
        // 抽屉页标题
        Drawer_title: "",
        // 是否新建描述(新建时，唯一标识可编辑，  其余状态，唯一标识不可编辑）
        is_create: false,
        // 编辑器是否编辑状态
        is_edit: false,
        // id , 如果是编辑时，就存储（关闭抽屉页时，需清除）
        description_id: ""

    }),
    actions: {
        // 新建create，查看view，编辑update
        showDescriptionFrom(formType, description_keywords = undefined) {
            if (formType === "create") {
                this.Drawer_title = "新增说明文档"
                this.descriptionDrawer = true
                this.is_edit = true
                this.is_create = true
            }
            if (formType === "view") {
                this.Drawer_title = "查看说明文档"
                this.is_edit = false

                console.log(description_keywords)
                getDescriptionListApi({keywords: description_keywords}).then(res => {
                    let descriptionData = res.data.data[0]
                    console.log(!descriptionData)
                    if (!descriptionData){
                        ElMessage.error("未找到该说明文档")
                        this.descriptionDrawer = false
                        return
                    }
                    this.descriptionDrawer = true
                    this.description_id = descriptionData.id
                    this.title = descriptionData.title
                    this.keywords = descriptionData.keywords
                    this.mavonValue = descriptionData.value
                })
            }
            if (formType === "update") {
                this.Drawer_title = "编辑说明文档"
                this.is_edit = true
                getDescriptionListApi({keywords: description_keywords}).then(res => {
                    let descriptionData = res.data.data[0]
                    this.descriptionDrawer = true
                    this.description_id = descriptionData.id
                    this.title = descriptionData.title
                    this.keywords = descriptionData.keywords
                    this.mavonValue = descriptionData.value
                })
            }
        },
        // 保存
        async commitDescriptionFrom() {
            if (this.description_id !== "") {
                console.log("编辑")
                putDescriptionApi(this.description_id,
                    {
                        title: this.title,
                        keywords: this.keywords,
                        value: this.mavonValue
                    }).then(res => {
                    ElMessage.success("保存成功");
                    this.description_id = res.id;
                    this.is_create = false;
                    this.is_edit = false;
                    this.Drawer_title = "查看说明文档"
                })

            } else {
                console.log("新增")
                if (this.keywords===""){
                    ElMessage.error("唯一标识不能为空");
                    return
                }
                postDescriptionApi({
                    title: this.title,
                    keywords: this.keywords,
                    value: this.mavonValue
                }).then(res => {
                    ElMessage.success("保存成功");
                    this.description_id = res.id;
                    this.is_create = false;
                    this.is_edit = false;
                    this.Drawer_title = "查看说明文档"
                })
            }
        },
        // 关闭
        async cancelDescriptionFrom() {
            this.descriptionDrawer = false;
            this.title = "";
            this.keywords = "";
            this.mavonValue = ""
            this.description_id = "";
            this.is_create = false;
            this.is_edit = false;

            $eventBus.emit("cancelDescriptionFrom")
        },
        // 抽屉页点击编辑，抽屉页中 转换当前的查看状态
        async editeDescription() {
            this.is_edit = !this.is_edit
            this.Drawer_title = "编辑说明文档"

        },

    }

})
